package top.xizai.anti.replay.base.req;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Optional;

/**
 * @NAME: WSC
 * @DATE: 2021/12/17
 * @DESCRIBE:
 **/
public class BaseRequest<T> extends HashMap<String, Object> {
    public Optional<T> getData() {
        var data = this.getOrDefault("data", "") + "";

        if (StringUtils.isNotBlank(data)) {
            Class<T> entityClass = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            var dataObj = JSON.parseObject(JSON.toJSONString(data), entityClass);
            return Optional.of(dataObj);
        }else {
            return Optional.empty();
        }
    }
}
